﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace AssemblyCYG
{
    public partial class CalPicture : UserControl
    {
        int calibIndex = 0;           //标定索引
        public delegate void GetPosHandler(int cIndex);
        public event GetPosHandler getPosHandler;
        public bool isCalibSuccess = false;
        public CalPicture()
        {
            InitializeComponent();
            HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
            HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 2);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            ho_AdaptiveImage(hWindowControl1, 4024, 3036);
        }
        private void ho_AdaptiveImage(HWindowControl hv_showImageControl, int hv_ImageWidth, int hv_ImageHeight)
        {
            double rWindow = hv_showImageControl.Width * 1.0f / hv_showImageControl.Height;
            double rImage = hv_ImageWidth * 1.0f / hv_ImageHeight;
            if (rWindow > rImage)
            {
                double nw = (hv_ImageWidth * rWindow / rImage - hv_ImageWidth) / 2;
                double showWidth = nw + hv_ImageWidth;
                HOperatorSet.SetPart(hv_showImageControl.HalconWindow, 0, nw * (-1), hv_ImageHeight, showWidth);
            }
            else
            {
                double nh = (hv_showImageControl.Height - hv_showImageControl.Width / rImage) * hv_ImageHeight / hv_showImageControl.Height;
                double showHeight = nh + hv_ImageHeight;
                HOperatorSet.SetPart(hv_showImageControl.HalconWindow, nh * (-1), 0, showHeight, hv_ImageWidth);
            }
        }
        public void SetCalLable(string str, int index)
        {
            this.lab_calName.Text = str;
            calibIndex = index;
        }
        public void SetCurPos(string str, Color color, bool isSuccess)
        {
            this.Invoke(new Action(() =>
            {
                this.lab_curPos.Text = str;
                this.lab_curPos.BackColor = color;
                isCalibSuccess = isSuccess;
            })); 

        }
        public void ShowImage(Bitmap bmp)
        {
            //this.pictureBox1.Image = bmp;
        }

        private void btn_getPos_Click(object sender, EventArgs e)
        {
            getPosHandler(calibIndex);
        }
    }
}
